<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <!--
       首先要进行双向绑定数据
       通过双向绑定获取数据向接口发送请求
       将返回的数据重新进行渲染
      -->
      <span class="search-label">员工姓名：</span>
      <el-input v-model="params.name" clearable placeholder="请输入员工姓名" class="search-main" @clear="getEmployeeList" />
      <el-button type="primary" class="search-btn" @click="searchInfo">查询</el-button>
    </div>
    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <el-button type="primary" @click="AddRole">添加员工</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="employeeList">
        <el-table-column type="index" label="序号" />
        <el-table-column label="员工信息" prop="name" />
        <el-table-column label="登录账号" prop="userName" />
        <el-table-column label="联系方式" prop="phonenumber" />
        <el-table-column label="角色" prop="roleName" />
        <el-table-column label="状态" prop="status" :formatter="formatter" />
        <el-table-column label="添加时间" prop="createTime" />
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="obj">
            <el-button size="mini" type="text">编辑</el-button>
            <el-button size="mini" type="text" @click="delEmployee(obj.row.id)">删除</el-button>
            <el-button size="mini" type="text">重置密码</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination
        :current-page="params.page"
        :page-size="params.pageSize"
        layout="total, prev, pager, next"
        :total="total"
      />
    </div>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-width="80px">
          <el-form-item label="员工姓名" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="登录账号" prop="userName">
            <el-input v-model="addForm.userName" />
          </el-form-item>
          <el-form-item label="联系方式" prop="phonenumber">
            <el-input v-model="addForm.phonenumber" />
          </el-form-item>
          <el-form-item label="分配角色" prop="roleId">
            <el-select v-model="addForm.roleId" placeholder="请选择角色">
              <el-option
                v-for="item in roleList"
                :key="item.roleId"
                :label="item.roleName"
                :value="item.roleId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="员工状态" prop="status">
            <el-radio-group v-model="addForm.status">
              <el-radio :label="0">禁用</el-radio>
              <el-radio :label="1">启用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="addComfire">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getEmployeeListAPI, AddEmployeeAPI, delEmployeeAPI } from '@/api/employee'
import { getRoleListAPI } from '@/api/system'

export default {
  name: 'Employee',
  data() {
    return {
      // 员工列表
      employeeList: [],
      // 获取角色数据
      roleList: [],
      // 分页
      params: {
        page: 1,
        pageSize: 2,
        name: '' // 员工姓名
      },
      total: 0,
      // 添加按钮的显示和隐藏
      dialogVisible: false,
      addForm: {
        name: '',
        phonenumber: '',
        roleId: '',
        status: 1,
        userName: ''
      },
      addFormRules: {
        name: [
          { required: true, message: '请输入员工姓名', trigger: 'blur' }
        ],
        userName: [
          { required: true, message: '请输入登录账号', trigger: 'blur' }
        ],
        phonenumber: [
          { required: true, message: '请输入联系方式', trigger: 'blur' }
        ],
        roleId: [
          { required: true, message: '请分配角色', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择员工状态', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    // 初始化员工列表
    this.getEmployeeList()
  },
  methods: {
    // 获取员工列表
    async getEmployeeList() {
      const res = await getEmployeeListAPI(this.params)
      console.log(res)
      // 员工列表
      this.employeeList = res.data.rows
      // 员工总数
      this.total = res.data.total
    },
    // 获取角色列表
    async getRoleList() {
      const res = await getRoleListAPI()
      console.log(res)
      this.roleList = res.data
    },
    // 员工状态
    formatter(row) {
      const mapping = {
        '0': '未启用',
        '1': '启用'
      }
      return mapping[row.status]
    },
    // 切换页面
    handleCurrentChange(value) {
      this.params.page = value
      this.getEmployeeList()
    },
    // 搜索功能
    searchInfo() {
      // 定有次功能是因为查找，要从第一页开始查找，否则数据可以查找不到
      this.params.page = 1
      this.getEmployeeList()
    },
    AddRole() {
      // 添加角色
      this.dialogVisible = true
      // 获取角色列表(也可以获取焦点的时候获取角色列表)
      this.getRoleList()
    },
    // 提交角色信息
    addComfire() {
      this.$refs.addForm.validate(async valid => {
        if (valid) {
          await AddEmployeeAPI(this.addForm)
          // 手动清除列表信息和自动清除列表信息
          this.$refs.addForm.resetFields()
          this.roleList = []
          // 关闭提示框
          this.dialogVisible = false
          // 刷新页面
          this.getEmployeeList()
          // 显示添加角色信息成功
          this.$message.success('添加角色成功')
        }
      })
    },
    // 删除角色信息
    async delEmployee(id) {
      this.$confirm('此操作将永久删除次员工, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delEmployeeAPI(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getEmployeeList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.Proportion{
  float: right;
  height: 30px;
  width: 400px;
  box-sizing: border-box;
  border: 1px solid #91d5ff;
  background-color: #e6f7ff;
  text-align: center;
  line-height: 30px;
  margin-top: 5px;
  font-size: 12px;
}
.Proportion > .el-icon-warning-outline{
  color: #1890ff;

  font-size: 14px;

}
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;
  .search-main {
    width: 220px;
    margin-right: 10px;
  }
  .search-btn{
    margin-left:20px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}

</style>
